package com.ruoyi.logistics.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.logistics.domain.po.ExplicitRegion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.logistics.domain.Region;
import com.ruoyi.logistics.service.IRegionService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;

/**
 * 详细地区Controller
 * 
 * @author ruoyi
 * @date 2023-08-06
 */
@RestController
@RequestMapping("/region")
public class RegionController extends BaseController
{
    @Autowired
    private IRegionService regionService;

    /**
     * 查询详细地区列表
     */
    @RequiresPermissions("logistics:region:list")
    @GetMapping("/list")
    public AjaxResult list(Region region)
    {
        List<Region> list = regionService.selectRegionList(region);
        return success(list);
    }

    /**
     * 查询详细地区列表
     */
    @RequiresPermissions("logistics:region:list")
    @GetMapping(value = "/listCenter/{level}")
    public AjaxResult list(@PathVariable("level") Integer level)
    {
        List<ExplicitRegion> list = regionService.selectReginCenterList(level);
        return success(list);
    }

    /**
     * 导出详细地区列表
     */
    @RequiresPermissions("logistics:region:export")
    @Log(title = "详细地区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Region region)
    {
        List<Region> list = regionService.selectRegionList(region);
        ExcelUtil<Region> util = new ExcelUtil<Region>(Region.class);
        util.exportExcel(response, list, "详细地区数据");
    }

    /**
     * 获取详细地区详细信息
     */
    @RequiresPermissions("logistics:region:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(regionService.selectRegionById(id));
    }

    /**
     * 获取详细地区详细信息包括物流中心信息
     */
    @RequiresPermissions("logistics:region:query")
    @GetMapping(value = "/Explicit/{id}")
    public AjaxResult getExplicitInfo(@PathVariable("id") Long id)
    {
        return success(regionService.selectExplicitRegionById(id));
    }

    /**
     * 根据id获取上级地区的详细信息
     */
    @RequiresPermissions("logistics:region:query")
    @GetMapping(value = "/parentCode/{id}")
    public AjaxResult getParentCodeInfo(@PathVariable("id") Long id)
    {
        System.out.println(1);
        return success(regionService.selectRegionByParentCode(id));
    }

    /**
     * 新增详细地区
     */
    @RequiresPermissions("logistics:region:add")
    @Log(title = "详细地区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Region region)
    {
        return toAjax(regionService.insertRegion(region));
    }

    /**
     * 修改详细地区
     */
    @RequiresPermissions("logistics:region:edit")
    @Log(title = "详细地区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Region region)
    {
        return toAjax(regionService.updateRegion(region));
    }

    /**
     * 删除详细地区
     */
    @RequiresPermissions("logistics:region:remove")
    @Log(title = "详细地区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(regionService.deleteRegionByIds(ids));
    }
}
